var RoomData = require("room");
var Poker = require("poker");

cc.Class({
    extends: cc.Component,

    properties: {
        headicon_perfab: cc.Prefab,
        choosecard_prefab: cc.Prefab,
        showcard_prefab: cc.Prefab,
        result_prefab: cc.Prefab,
    },
    
    room: null,
    showResult: false,

    onLoad: function () {
        this.initRoomData();
    },
    
    //初始化房间信息
    initRoomData: function () {
        var gamedata_node = cc.find("GameData");
        var gamedata_component = gamedata_node.getComponent("Game_Data");
        
        var text_gamemode;
        var game_mode = gamedata_component.roomdata.getMode();
        if (game_mode === 4) {
            text_gamemode = "四人场";
        } else if (game_mode === 3){
            text_gamemode = "三人场";
        }
        var game_round = gamedata_component.roomdata.getRounds();
        var game_rid = gamedata_component.roomdata.getRid();
        var game_mapai = gamedata_component.roomdata.getMapai();
        
        var moshi_node = cc.find("spr_bg/label_moshi", this.node);
        moshi_node.getComponent(cc.Label).string = "模式:  " + text_gamemode;
        var jushu_node = cc.find("spr_bg/label_jushu", this.node);
        jushu_node.getComponent(cc.Label).string = "局数:  " + game_round + "局";
        var fanghao_node = cc.find("spr_bg/label_fanghao", this.node);
        fanghao_node.getComponent(cc.Label).string = "房号:  " + game_rid;
        if (gamedata_component.roomdata.getMapai()) {
            var mapai_spr_node = cc.find("spr_bg/label_mapai/spr_mapai", this.node);
            switch (game_mapai) {
                case 53:
                    mapai_spr_node.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("poker_53");
                    break;
                case 103:
                    mapai_spr_node.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("poker_103");
                    break;
                case 143:
                    mapai_spr_node.getComponent(cc.Sprite).spriteFrame = cc.spriteFrameCache.getSpriteFrame("poker_143");
                    break;
            }
        } else {
            var mapai_node = cc.find("spr_bg/label_mapai", this.node);
            mapai_node.active = false;
        }
        
        this.room = new RoomData(game_mapai, game_mode, game_round, game_rid);
        this.initUserData(123);
    },
    
    //添加用户
    initUserData: function (uid) {
        this.room.addUser(uid);
        var myHeadicon = cc.instantiate(this.headicon_perfab);
        myHeadicon.name = "myHead_prefab";
        myHeadicon.getComponent("headicon_component").username = "范范之辈";
        myHeadicon.getComponent("headicon_component").score = 0;
        var headpos1 = cc.find("spr_bg/position_head/player_0", this.node);
        headpos1.addChild(myHeadicon);
        
        var myHeadicon1 = cc.instantiate(this.headicon_perfab);
        myHeadicon1.name = "myHead_prefab1";
        myHeadicon1.getComponent("headicon_component").username = "测试大王";
        myHeadicon1.getComponent("headicon_component").score = 0;
        var headpos2 = cc.find("spr_bg/position_head/player_1", this.node);
        headpos2.addChild(myHeadicon1);
        
        var myHeadicon2 = cc.instantiate(this.headicon_perfab);
        myHeadicon2.name = "myHead_prefab2";
        myHeadicon2.getComponent("headicon_component").username = "我来撑场面";
        myHeadicon2.getComponent("headicon_component").score = 0;
        var headpos3 = cc.find("spr_bg/position_head/player_2", this.node);
        headpos3.addChild(myHeadicon2);
        
        var myHeadicon3 = cc.instantiate(this.headicon_perfab);
        myHeadicon3.name = "myHead_prefab3";
        myHeadicon3.getComponent("headicon_component").username = "这个是啥";
        myHeadicon3.getComponent("headicon_component").score = 0;
        var headpos4 = cc.find("spr_bg/position_head/player_3", this.node);
        headpos4.addChild(myHeadicon3);
    },
    
    //准备
    onReady: function () {
        cc.log("onReady callback");
        var headpos1 = cc.find("spr_bg/position_head/player_0", this.node);
        headpos1.getChildByName("myHead_prefab").getComponent("headicon_component").ready = 1;
        headpos1.getChildByName("myHead_prefab").getComponent("headicon_component").updateHeadMessage();
        
        var btn_ready = cc.find("spr_bg/ready", this.node);
        btn_ready.active = false;
        
        this.node.runAction(cc.sequence(
            cc.delayTime(1),
            cc.callFunc(this.onPullHeadCards, this)
            ));
    },
    
    //下一局
    onNextRound: function () {
        if (this.node.getChildByName("result_node")) {
            this.node.getChildByName("result_node").destroy();
        }
        for (let i = 0; i < 4; i++) {
            var cardpos_node = cc.find("spr_bg/position_card/card_" + i, this.node);
            cardpos_node.getChildByName("showcard_node" + (i + 1)).destroy();
        }
        this.showResult = false;
        
        var headpos1 = cc.find("spr_bg/position_head/player_0", this.node);
        headpos1.getChildByName("myHead_prefab").getComponent("headicon_component").ready = 1;
        headpos1.getChildByName("myHead_prefab").getComponent("headicon_component").updateHeadMessage();
        
        this.node.runAction(cc.sequence(
            cc.delayTime(1),
            cc.callFunc(this.onPullHeadCards, this)
            ));
    },
    
    //语音
    onVoice: function () {
        cc.log("onVoice callback");
    },
    
    //退出
    onExit: function () {
        cc.log("onExit callback");
        cc.director.loadScene("lobby_scene", function (err, scene) {
            if (err) {
                cc.warn("load lobby_scene err");
            }
        });
    },
    
    //聊天
    onChat: function () {
        cc.log("onChat callback");
    },
    
    //进入选牌
    onPullHeadCards: function () {
        var headpos1 = cc.find("spr_bg/position_head/player_0", this.node);
        headpos1.getChildByName("myHead_prefab").getComponent("headicon_component").ready = 0;
        headpos1.getChildByName("myHead_prefab").getComponent("headicon_component").updateHeadMessage();
        /**
         * 0-3 2, 4-7 3, 8-11 4, 12-15 5, 16-19 6, 20-23 7, 24-27 8, 28-31 9
         * 32-35 10, 36-39 J, 40-43 Q, 44-47 K, 48-51 A
         */
        var cards = [0,4,8,12,16,35,37,38,39,3,43,9,51];
        cards.sort(function (a, b) {
            return a - b;
        });
        var choosecard = cc.instantiate(this.choosecard_prefab);
        choosecard.name = "choosecard_component";
        choosecard.getComponent("choosecard_component").gameNode = this;
        choosecard.getComponent("choosecard_component").handCards = cards;
        this.node.addChild(choosecard);
    },
    
    //完成选牌
    onFinishChooseCard: function (arr1, arr2, arr3) {
        if (this.node.getChildByName("choosecard_component")) {
            this.node.getChildByName("choosecard_component").destroy();
        }
        this.onShowArray(arr1, arr2, arr3);
    },
    
    //显示牌组
    onShowArray: function (arr1, arr2, arr3) {
        var showcard_node1 = cc.instantiate(this.showcard_prefab);
        showcard_node1.name = "showcard_node1";
        showcard_node1.getComponent("showcard_component").gameNode = this;
        showcard_node1.getComponent("showcard_component").showtoudaoarray = arr1;
        showcard_node1.getComponent("showcard_component").showzhongdaoarray = arr2;
        showcard_node1.getComponent("showcard_component").showweidaoarray = arr3;
        showcard_node1.getComponent("showcard_component").leftOrright = true;
        var cardpos1 = cc.find("spr_bg/position_card/card_0", this.node);
        cardpos1.addChild(showcard_node1);
        
        var showcard_node2 = cc.instantiate(this.showcard_prefab);
        showcard_node2.name = "showcard_node2";
        showcard_node2.getComponent("showcard_component").gameNode = this;
        showcard_node2.getComponent("showcard_component").showtoudaoarray = arr1;
        showcard_node2.getComponent("showcard_component").showzhongdaoarray = arr2;
        showcard_node2.getComponent("showcard_component").showweidaoarray = arr3;
        showcard_node2.getComponent("showcard_component").leftOrright = false;
        var cardpos2 = cc.find("spr_bg/position_card/card_1", this.node);
        cardpos2.addChild(showcard_node2);
        
        var showcard_node3 = cc.instantiate(this.showcard_prefab);
        showcard_node3.name = "showcard_node3";
        showcard_node3.getComponent("showcard_component").gameNode = this;
        showcard_node3.getComponent("showcard_component").showtoudaoarray = arr1;
        showcard_node3.getComponent("showcard_component").showzhongdaoarray = arr2;
        showcard_node3.getComponent("showcard_component").showweidaoarray = arr3;
        showcard_node3.getComponent("showcard_component").leftOrright = true;
        var cardpos3 = cc.find("spr_bg/position_card/card_2", this.node);
        cardpos3.addChild(showcard_node3);
        
        var showcard_node4 = cc.instantiate(this.showcard_prefab);
        showcard_node4.name = "showcard_node4";
        showcard_node4.getComponent("showcard_component").gameNode = this;
        showcard_node4.getComponent("showcard_component").showtoudaoarray = arr1;
        showcard_node4.getComponent("showcard_component").showzhongdaoarray = arr2;
        showcard_node4.getComponent("showcard_component").showweidaoarray = arr3;
        showcard_node4.getComponent("showcard_component").leftOrright = false;
        var cardpos4 = cc.find("spr_bg/position_card/card_3", this.node);
        cardpos4.addChild(showcard_node4);
    },
    
    //显示结算界面
    onShowRoundResult: function () {
        if (!this.showResult) {
            this.showResult = true;
            var result_node = cc.instantiate(this.result_prefab);
            result_node.name = "result_node";
            result_node.getComponent("result_component").gameNode = this;
            this.node.addChild(result_node);
        }
    },
});
